package dao;

import model.Level;
import model.Player;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

import java.util.List;

/**
 * Created by VLAD on 07.07.2014.
 */
public class PlayerDao implements IPlayerDao {
    @Override
    public long savePlayer(Player player)
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Long playerID = null;
            tx = session.beginTransaction();
            playerID = (Long)session.save(player);
            tx.commit();
        return playerID;
    }

    @Override
    public List getAllPlayers()
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        tx = session.beginTransaction();
        List players = session.createQuery("FROM model.Player").list();
        tx.commit();
        return players;
    }



    @Override
    public void updatePlayer(Player player) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Long playerID = null;
        tx = session.beginTransaction();
        session.update(player);
        tx.commit();
    }

    @Override
    public Player getPlayerByID(long id) {
        Player player = null;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Long playerID = null;
        tx = session.beginTransaction();
        player = (Player)session.get(Player.class, id);
        tx.commit();
        return player;
    }

    @Override
    public List<Player> getAllByRatingOrder(int listCapacity) {
        String query = "FROM model.Player P ORDER BY P.rating.rating DESC";
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        tx = session.beginTransaction();
        List players = session.createQuery(query).setMaxResults(listCapacity).list();
        tx.commit();
        //System.out.println(players);
        return players;
    }

    @Override
    public long getPlayerPositionInRating(Player player) {
        String query = "SELECT COUNT(*) FROM model.Player P WHERE P.rating.rating <= "+player.getRating().getRating()+" ORDER BY P.rating.rating";
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        tx = session.beginTransaction();
        List position = session.createQuery(query).list();
        tx.commit();
        System.out.println(position);
        return getPlayersCount() - (Long) position.get(0) + 1;

    }

    private long getPlayersCount()
    {
        String query = "SELECT COUNT(*) FROM model.Player P";
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        tx = session.beginTransaction();
        List count = session.createQuery(query).list();
        tx.commit();
        return (Long) count.get(0);
    }


}
